package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleScoreMapper;
import com.heima.article.service.AIChatService;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleClassifyService;
import com.heima.article.service.ArticleScoreService;
import com.heima.model.article.dtos.ScoreDto;
import com.heima.model.article.dtos.UserDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ArticleUserScore;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ArticleClassifyServiceImpl
 * @Description TODO
 * @Author Neo
 * @Date 2024/3/7 22:10
 * @Version 1.0
 */
@Component
public class ArticleScoreServiceImpl extends ServiceImpl<ArticleScoreMapper, ArticleUserScore> implements ArticleScoreService  {

    @Autowired
    private ArticleScoreMapper articleScoreMapper;
    @Override
    public ResponseResult<List<ArticleUserScore>> getScoreData(UserDto userDto) {
        LambdaQueryWrapper<ArticleUserScore> wrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ArticleUserScore> wrapper2 = new LambdaQueryWrapper<>();
        List<ArticleUserScore> res=new ArrayList<>();
        if(userDto!=null&&StringUtils.isNotBlank(userDto.getUserId())){
            Long userId = Long.valueOf(userDto.getUserId());
            wrapper1.eq(ArticleUserScore::getUserId,userDto.getUserId());
            wrapper1.last("LIMIT 100");
            wrapper2.ne(ArticleUserScore::getUserId,userDto.getUserId());
            wrapper2.last("LIMIT 300");

            List<ArticleUserScore> list = list(wrapper1);
            List<ArticleUserScore> list2 = list(wrapper2);
            res.addAll(list);
            res.addAll(list2);
        }else{
            wrapper2.last("LIMIT 400");
            List<ArticleUserScore> list2 = list(wrapper2);
            res.addAll(list2);
        }
        return ResponseResult.okResult(res);
    }

    @Override
    public ResponseResult<List<ArticleUserScore>> Score(ScoreDto scoreDto) {
        if(scoreDto==null||scoreDto.getArticleId()==null||scoreDto.getScore()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        QueryWrapper<ArticleUserScore> articleUserScoreQueryWrapper = new QueryWrapper<>();
        articleUserScoreQueryWrapper.eq("user_id",scoreDto.getUserId());
        articleUserScoreQueryWrapper.eq("article_id",scoreDto.getArticleId());
        ArticleUserScore articleUserScore1 = articleScoreMapper.selectOne(articleUserScoreQueryWrapper);
        if(articleUserScore1==null){
            ArticleUserScore articleUserScore = new ArticleUserScore();
            articleUserScore.setArticleId(scoreDto.getArticleId());
            articleUserScore.setScore(scoreDto.getScore());
            articleUserScore.setUserId(scoreDto.getUserId());
            int insert = articleScoreMapper.insert(articleUserScore);
            if(insert==1){
                return ResponseResult.okResult(null);
            }else {
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }
        }else{
            articleUserScore1.setScore(scoreDto.getScore());
            int update = articleScoreMapper.updateById(articleUserScore1);
            if(update==1){
                return ResponseResult.okResult(null);
            }else {
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }
        }
    }
}
